package com.hison.tandainbao.response;

import com.alibaba.fastjson.JSON;
import com.hison.tandainbao.enums.ResultEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;

/**
 * @ClassName: ResponseResult
 * @Descripiton: 返回体封装类
 * @Author: 小郑
 * @Date： 2024/1/28 15:00
 * @Version：1.8
 **/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResponseResult implements Serializable {
    private static final Logger logger = LoggerFactory.getLogger(ResponseResult.class);
    private static final long serialVersionUID = 1L;
    /**
     * 响应业务状态码
     */
    private Integer code;
    /**
     * 响应信息
     */
    private String message;
    /**
     * 响应中的数据
     */
    private Object data;

    public static ResponseResult ok() {
        return new ResponseResult(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getDesc(),
                null);
    }

    public static ResponseResult ok(Object data) {
        return new ResponseResult(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getDesc(),
                data);
    }

    public static ResponseResult ok(String message, Object data) {
        return new ResponseResult(ResultEnum.SUCCESS.getCode(), message, data);
    }

    public static ResponseResult error(String message) {
        logger.debug("返回错误：code={}, message={}", ResultEnum.ERROR.getCode(), message);
        return new ResponseResult(ResultEnum.ERROR.getCode(), message, null);
    }

    public static ResponseResult build(int code, String message) {
        logger.debug("返回结果：code={}, message={}", code, message);
        return new ResponseResult(code, message, null);
    }

    public static ResponseResult build(ResultEnum resultEnum) {
        logger.debug("返回结果：code={}, message={}", resultEnum.getCode(),
                resultEnum.getDesc());
        return new ResponseResult(resultEnum.getCode(), resultEnum.getDesc(), null);
    }


    public String  toJsonString(){
        return JSON.toJSONString(this);
    }
}